package com.seu.project.seuquiz.model.converter;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seu.project.seuquiz.model.dto.question.QuestionUpdateRequest;
import com.seu.project.seuquiz.model.entity.Question;
import com.seu.project.seuquiz.model.internal.QuestionSelectionInternal;
import com.seu.project.seuquiz.model.vo.QuestionVO;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author SoftPro
 * @createDate: 2023-07-16 11:03
 */
public class QuestionConverter {

    /**
     * 将 QuestionUpdateRequest 更新对象转换为 Question 实体类
     * @param request
     * @return
     */
    public static Question questionUpdateRequestToQuestion(QuestionUpdateRequest request) {
        if (request == null) {
            return null;
        }

        return Question.builder()
                .id(request.getId())
                .content(request.getContent())
                .selection(selectionInternalToSelection(request.getSelection()))
                .answer(request.getAnswer())
                .remark(request.getRemark())
                .score(request.getScore())
                .questionType(request.getQuestionType())
                .type(request.getType())
                .difficulty(request.getDifficulty())
                .correctCnt(request.getCorrectCnt())
                .writeCnt(request.getWriteCnt())
                .status(request.getStatus())
                .build();
    }

    public static String selectionInternalToSelection(QuestionSelectionInternal[] selectionArray) {
        StringBuilder selection = new StringBuilder();
        for (int i = 0; i < selectionArray.length; i++) {
            selection.append(selectionArray[i].getSelect() + selectionArray[i].getSelectContent());
            if (i != selectionArray.length - 1) selection.append("#");
        }
        return selection.toString();
    }

    public static QuestionSelectionInternal[] selectionToSelectionEnum(String selection) {
        String[] split = selection.split("#");
        QuestionSelectionInternal[] questionSelectionInternals = new QuestionSelectionInternal[split.length];
        for (int i = 0; i < split.length; i++) {
            QuestionSelectionInternal questionSelection = new QuestionSelectionInternal();
            questionSelection.setSelect(split[i].length() > 0 ? (split[i].charAt(0) + "") : "");
            questionSelection.setSelectContent(split.length > 1 ? split[i].substring(1) : "");
            questionSelectionInternals[i] = questionSelection;
        }
        return questionSelectionInternals;
    }

/*    public static void main(String[] args) {
        String selection = "dsfdsdfs#vfsdf#fsd";
        List<String> stringList = selectionToSelectionList(selection);
        System.out.println(stringList);

        String s = selectionListToSelection(stringList);
        System.out.println(s);
    }*/

    public static QuestionVO toQuestionVO(Question question) {

        return QuestionVO.builder()
                .id(question.getId())
                .content(question.getContent())
                .selection(selectionToSelectionEnum(question.getSelection()))
                .answer(question.getAnswer())
                .analysis(question.getAnalysis())
                .remark(question.getRemark())
                .score(question.getScore())
                .questionType(question.getQuestionType())
                .type(question.getType())
                .difficulty(question.getDifficulty())
                .correctCnt(question.getCorrectCnt())
                .writeCnt(question.getWriteCnt())
                .status(question.getStatus())
                .createTime(question.getCreateTime())
                .updateTime(question.getUpdateTime())
                .build();
    }

    public static Page<QuestionVO> questionPageToQuestionVOPage(Page<Question> questionPage) {
        List<Question> records = questionPage.getRecords();
        List<QuestionVO> questionVOList = records.stream().map(it -> toQuestionVO(it)).sorted((o1, o2) -> {return o2.getCreateTime().compareTo(o1.getCreateTime());}).collect(Collectors.toList());

        Page<QuestionVO> questionVOPage = new Page<>();
        BeanUtils.copyProperties(questionPage,questionVOPage);
        questionVOPage.setRecords(questionVOList);
        return questionVOPage;
    }
}
